Key_Down equ 0  ;These are constants, representing the bit that will be reset
                    ;if the corresponding key is pressed.  Used for testing
                    ;which key is pressed.
Key_Up equ 3
Key_Left equ 1
Key_Right equ 2

Group_Arrow_Keys equ $FE

Key_2nd equ 5
Key_Mode equ 6
Key_Del equ 7
Key_Math equ 6
Key_Apps equ 6
Key_Pgrm equ 6
Key_Alpha equ 7
Key_Stat equ 7

Group_2nd_Mode_Del equ $BF
Group_Alpha_Math equ $DF
Group_Apps equ $EF
Group_Stat_Pgrm equ $F7

West equ 0
Northwest equ 1
North equ 2
Northeast equ 3
East equ 4
Southeast equ 5
South equ 6
Southwest equ 7



;This file handles keyboard matters when a unit is in attack mode.  

;Variables:
;Player_1_Ship -- The unit that the player is using
;Attack_Mode -- Boolean.  If Attack Mode is on, keys are used differently than when used for Standard Mode.
;Player1_X
;Player1_Y
;Player1_Turret_Position, where 0 is equal to pointing West




Get_Keys:

 LD C, 1 ;Port for Keypressing

 LD A, $FF
 OUT (C), A ;Reset Port
 NOP
 NOP

 ;First, check to see if any Arrow Keys have been pressed.  These, of
 ;course, move the unit.

 LD A, Group_Arrow_Keys
 OUT (C),A
 NOP
 NOP
 IN A, (C)

 CP %11111110
 CALL Z, Move_Unit_South
 CP %11111101
 CALL Z, Move_Unit_West
 CP %11111011
 CALL Z, Move_Unit_East
 CP %11110111
 CALL Z, Move_Unit_North
 CP %11111100
 CALL Z, Move_Unit_Southwest
 CP %11111010
 CALL Z, Move_Unit_Southeast
 CP %11110101
 CALL Z, Move_Unit_Northwest
 CP %11110011
 CALL Z, Move_Unit_Northeast
 
 LD E,A
 
 
 LD B, $FF
 OUT (C), B ;Reset Port.  A is tied up at the moment, so B is used instead
 NOP
 NOP

 LD B, Group_2nd_Mode_Del
 OUT (C), B
 NOP
 NOP
 IN B, (C)
 
 
 BIT Key_2nd, B
 CALL Z, Set_Turret_Position_Northwest
 BIT Key_Mode, B
 CALL Z, Set_Turret_Position_North
 BIT Key_Del, B
 CALL Z, Set_Turret_Position_Northeast
 
 
 LD B, $FF
 OUT (C), B ;Reset Port.  A is tied up at the moment, so B is used instead
 NOP
 NOP

 LD B, Group_Alpha_Math
 OUT (C), B
 NOP
 NOP
 IN B, (C)
 
 BIT Key_Alpha, B
 CALL Z, Set_Turret_Position_West
 BIT Key_Math, B
 CALL Z, Set_Turret_Position_Southwest
 
 LD B, $FF
 OUT (C), B ;Reset Port.  A is tied up at the moment, so B is used instead
 NOP
 NOP

 LD B, Group_Apps
 OUT (C), B
 NOP
 NOP
 IN B, (C)
 
 BIT Key_Apps, B
 CALL Z, Set_Turret_Position_South
 
 LD B, $FF
 OUT (C), B ;Reset Port.  A is tied up at the moment, so B is used instead
 NOP
 NOP

 LD B, Group_Stat_Pgrm
 OUT (C), B
 NOP
 NOP
 IN B, (C) 
 
 BIT Key_Stat, B
 CALL Z, Set_Turret_Position_East
 BIT Key_Pgrm, B
 CALL Z, Set_Turret_Position_Southeast
 

 LD A,E
 XOR 255
 
 JR NZ, No_Need_To_Zero_Walking_Frame
 
 ;In the case of resetting the walking motion of a unit,
 ;Walking_Sprite_Frame will be reset to one, but A will be set to 2 to draw
 ;a still Scouter
 
 LD A,1
 LD (Walking_Sprite_Frame), A
 LD E, 2
 CALL Draw_Unit
 RET
 
 

;This next procedure determines what the player's unit is, and calls the respective function for drawing it.  This is because some units 
;require only one or two sprites, whereas others require many, many more.
No_Need_To_Zero_Walking_Frame:
 
 LD A, (Walking_Sprite_Frame)
 XOR 1
 LD (Walking_Sprite_Frame), A
 LD E,A
 CALL Draw_Unit

 RET


Move_Unit_South:

;Moves the unit down one pixel
 
 LD HL, Player1_Y
 INC (HL)
 LD A, South
 LD (Player1_Direction), A
 RET

Move_Unit_East:

 LD HL, Player1_X
 INC (HL)
 LD A, East
 LD (Player1_Direction), A
 RET

Move_Unit_West:

 LD HL, Player1_X
 DEC (HL)
 LD A, West
 LD (Player1_Direction), A
 RET

Move_Unit_North:

 LD HL, Player1_Y
 DEC (HL)
 LD A, North
 LD (Player1_Direction), A
 RET

Move_Unit_Northwest:

 LD HL, Player1_Y
 DEC (HL)
 LD HL, Player1_X
 DEC (HL)
 LD A, Northwest
 LD (Player1_Direction), A
 RET

Move_Unit_Northeast:

 LD HL, Player1_Y
 DEC (HL)
 LD HL, Player1_X
 INC (HL)
 LD A, Northeast
 LD (Player1_Direction), A
 RET

Move_Unit_Southwest:

 LD HL, Player1_Y
 INC (HL)
 LD HL, Player1_X
 DEC (HL)
 LD A, Southwest
 LD (Player1_Direction), A
 RET

Move_Unit_Southeast:

 LD HL, Player1_Y
 INC (HL)
 LD HL, Player1_X
 INC (HL)
 LD A, Southeast
 LD (Player1_Direction), A
 RET


Set_Turret_Position_West:

 LD A, West
 LD (Player1_Turret_Position), A
 RET
 
Set_Turret_Position_Northwest:

 LD A, Northwest
 LD (Player1_Turret_Position), A
 RET
 
Set_Turret_Position_North:

 LD A, North
 LD (Player1_Turret_Position), A
 RET
 
Set_Turret_Position_Northeast:

 LD A, Northeast
 LD (Player1_Turret_Position), A
 RET
 
Set_Turret_Position_East:

 LD A, East
 LD (Player1_Turret_Position), A
 RET
 
Set_Turret_Position_Southeast:

 LD A, Southeast
 LD (Player1_Turret_Position), A
 RET
 
Set_Turret_Position_South;

 LD A, South
 LD (Player1_Turret_Position), A
 RET
 
Set_Turret_Position_Southwest:


 LD A, Southwest
 LD (Player1_Turret_Position), A
 RET
 